﻿Public Class Form_alta_cot
    Dim estado As String = "estado_nuevo"

    Private Sub Form_alta_cot_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.CenterToScreen()
        agregar_detalle()
        guardar_detalle()
    End Sub



    Private Sub bt_guardar_Click(sender As Object, e As EventArgs) Handles bt_guardar.Click
        Try
            If estado = "estado_nuevo" Then
                Using updateTransaction As New Transactions.TransactionScope
                    ' Add code to save your data here.
                    nueva_cotizacion()
                    ' Throw an exception to roll back the transaction. 
                    ' Call the Complete method to commit the transaction
                    updateTransaction.Complete()
                End Using
                Me.Close()



            ElseIf estado = "copiar" Then

            End If
        Catch ex As Exception
            ex.ToString()
        End Try
    End Sub

    Private Sub bt_cancelar_Click(sender As Object, e As EventArgs)
        If estado = "modificacion" Then
            If MessageBox.Show("Desea cancelar la modificacion?", "Atencion", MessageBoxButtons.YesNo, MessageBoxIcon.Stop) = Windows.Forms.DialogResult.Yes Then

            End If
        ElseIf estado = "estado_nuevo" Then
            If MessageBox.Show("Desea cancelar la carga de la cotización?", "Atención", MessageBoxButtons.YesNo, MessageBoxIcon.Stop) = Windows.Forms.DialogResult.Yes Then
                'estado_inicial()
            Else

            End If
        End If
    End Sub

    Sub nueva_cotizacion()

        Dim Cmd As SqlClient.SqlCommand ' objeto comando para ejecutar sentencias sql
        Dim sbQuery As String
        
        Dim cliente As String = tx_cliente_a.Text
        Dim cod_transporte As String = tx_codigo_transporte.Text
        Dim vendedor As String = tx_vendedor.Text
        Dim cond_venta As String = tx_cond_venta.Text
        Dim observaciones As String = tx_observaciones.Text
        Dim bonificacion As String = tx_bonificacion.Text

        Try
            Conectar()
            sbQuery = "INSERT INTO GVA08 VALUES (" & _
                        "''" & _
                        ", ''" & _
                        ", ''" & _
                        "," & "'" & cliente & "'" & _
                        ", ''" & _
                        "," & cod_transporte & _
                        "," & "'" & vendedor & "'" & _
                        "," & cond_venta & _
            ",1.0000000" & _
            ",3" & _
                        ", '1800-01-01 00:00:00.000'" & _
                        ", '1800-01-01 00:00:00.000'" & _
                        ", (select getdate())" & _
                        ", '1800-01-01 00:00:00.000'" & _
                        ", (select getdate())" & _
                        ", (select getdate())" & _
                        ", (select getdate())" & _
                        ", ''" & _
                        ", (select convert(varchar,DATEPART (hour,GETDATE())) + (select convert(varchar,DATEPART (minute,GETDATE()))))" & _
            ", 0.0000000" & _
            ", 8429.7500000" & _
            ", 8429.7500000" & _
            ", 0.0000000" & _
            ", 0.0000000" & _
            ", 0.0000000" & _
            ", 1770.2500000" & _
            ", 0.0000000" & _
                        ", 'FORMAS DE PAGO: sin'" & _
                        ", 'CONTACTO Y TEL:'" & _
                        ", 'PLAZO DE ENTREGA:'" & _
                        ", 'DOMICILIO Y HOR:'" & _
                        ", 'MANTENIMIENTO OFERTA:'" & _
                        ", 1" & _
                        ", ''" & _
                        ", (select max (N_COTIZ) + 1 from GVA08)" & _
                        ", 2" & _
                        "," & "'" & observaciones & "'" & _
                        ", 0" & _
                        "," & bonificacion & _
                        ", (select max (TALONARIO) from GVA08)" & _
                        ", 'H'" & _
                        ", 'informes'" & _
                        ", 'informes'" & _
                        ", (select convert(varchar,DATEPART (hour,GETDATE())) + (select convert(varchar,DATEPART (minute,GETDATE()))) + (select convert(varchar,DATEPART (second,GETDATE()))))" & _
                        ", ''" & _
                        ", (select getdate())" & _
                        ", (select convert(varchar,DATEPART (hour,GETDATE())) + (select convert(varchar,DATEPART (minute,GETDATE()))) + (select convert(varchar,DATEPART (second,GETDATE()))))" & _
                        ", 'informes'" & _
                        ", 'TERMINAL X'" & _
                        ", '1800-01-01 00:00:00.000'" & _
                        ", ''" & _
                        ", ''" & _
                        ", ''" & _
                        ", '1800-01-01 00:00:00.000'" & _
                        ", '1800-01-01 00:00:00.000'" & _
                        ", ''" & _
                        ", NULL)"
            Cmd = New SqlClient.SqlCommand(sbQuery, cn)
            'Adp = New SqlClient.SqlDataAdapter(Cmd)
            Cmd.ExecuteNonQuery()
            MessageBox.Show("La cotización ha sido guardada", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


#Region "key_press"
    Private Sub tx_cotizacion_a_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Asc(e.KeyChar) = 13 Then
            tx_cotizacion_b.Focus()
        End If
    End Sub
    Private Sub tx_cotizacion_b_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Asc(e.KeyChar) = 13 Then
            cb_estado.Focus()
        End If
    End Sub
    Private Sub cb_estado_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Asc(e.KeyChar) = 13 Then
            dt_fecha.Focus()
        End If
    End Sub
    Private Sub dt_fecha_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Asc(e.KeyChar) = 13 Then
            bt_traer_cliente.Focus()
        End If
    End Sub
    Private Sub bt_traer_cliente_Click(sender As Object, e As EventArgs)
        Me.tx_cliente_b.Focus()
    End Sub
    Private Sub tx_cliente_b_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Asc(e.KeyChar) = 13 Then
            dt_vigencia.Focus()
        End If
    End Sub
    Private Sub dt_vigencia_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Asc(e.KeyChar) = 13 Then
            tx_cond_venta.Focus()
        End If
    End Sub
    Private Sub tx_cond_venta_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Asc(e.KeyChar) = 13 Then
            cb_vendedor.Focus()
        End If
    End Sub
    Private Sub cb_vendedor_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Asc(e.KeyChar) = 13 Then
            tx_bonificacion.Focus()
        End If
    End Sub
    Private Sub tx_bonificacion_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Asc(e.KeyChar) = 13 Then
            tx_lista_precios.Focus()
        End If
    End Sub
    Private Sub tx_lista_precios_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Asc(e.KeyChar) = 13 Then
            tx_codigo_transporte.Focus()
        End If
    End Sub
    Private Sub tx_codigo_transporte_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Asc(e.KeyChar) = 13 Then
            tx_moneda_a.Focus()
        End If
    End Sub
    Private Sub tx_moneda_a_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Asc(e.KeyChar) = 13 Then
            tx_moneda_b.Focus()
        End If
    End Sub
#End Region

   
    Private Sub bt_modificar_Click(sender As Object, e As EventArgs) Handles bt_modificar.Click

    End Sub

    Private Sub bt_copiar_Click(sender As Object, e As EventArgs) Handles bt_copiar.Click
        If MessageBox.Show("Desea copiar la cotización?", "Atencion", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            MessageBox.Show("Los datos de la cotizacion se han copiado. Al guardar se generará una nueva cotización?", "Atencion", MessageBoxButtons.OK, MessageBoxIcon.Information)
            estado = "copiar"
        End If
    End Sub

    Private Sub bt_cancelar_Click_1(sender As Object, e As EventArgs) Handles bt_cancelar.Click

        If MessageBox.Show("Desea copiar la cotización?", "Atencion", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            MessageBox.Show("Los datos de la cotizacion se han copiado. Al guardar se generará una nueva cotización?", "Atencion", MessageBoxButtons.OK, MessageBoxIcon.Information)
            estado = "copiar"
        End If
    End Sub

    Private Sub bt_articulo_agr_Click(sender As Object, e As EventArgs) Handles bt_articulo_agr.Click
        If Form_articulos.ShowDialog = Windows.Forms.DialogResult.OK Then

        End If
    End Sub

    Private Sub agregar_detalle()
        Me.DataGridView2.Rows.Clear()
        Dim array As String = "ESCSOL1, ESCRITORIO L-180 PREMIER, 2, UNI, 433.8200000, 0.00, 867.6400000"
        Dim temp_Array() As String = array.Split(","c)
        Me.DataGridView2.Rows.Add(temp_Array)
        array = "BIBMIGTURIN, BIBLIOTECA BAJA 1.80X0.45X0.83, 1, UNI, 200.0000000, 0.00, 200.0000000"
        temp_Array = array.Split(","c)
        Me.DataGridView2.Rows.Add(temp_Array)
        MessageBox.Show(Me.DataGridView2.Rows.Count)
        'Me.DataGridView2.Rows.Remove(Me.DataGridView2.Rows(2))
    End Sub
    Private Sub guardar_detalle()
        Dim Cmd As SqlClient.SqlCommand
        Dim sbQuery As String
        Conectar()
        Try
            For i = 0 To Me.DataGridView2.Rows.Count - 2
                sbQuery = "INSERT INTO GVA09 VALUES (" & _
                            "''" & _
                            "," & Me.DataGridView2.Item(2, i).Value() & "')" & _
                            "," & Me.DataGridView2.Item(2, i).Value() & "')" & _
                            "," & "'" & Me.DataGridView2.Item(0, i).Value() & "'" & _
                            ", 1.0000000" & _
                            ", 3" & _
                            ", (SELECT PRECIO * " & Me.DataGridView2.Item(2, i).Value() & "from gva17 where COD_ARTICU = '" & Me.DataGridView2.Item(0, i).Value() & "')" & _
                            ", select CONCAT (' 000',(select max(N_COTIZ) + 1  from GVA08))" & _
                            "," & i + 1 & _
                            ", 0.0000000" & _
                            ", (SELECT PRECIO from gva17 where COD_ARTICU = '" & Me.DataGridView2.Item(0, i).Value() & "')" & _
                            ",7" & _
                            ", ''" & _
                            ", 0.0000000" & _
                            ", 0.0000000" & _
                            ", (SELECT ID_MEDIDA_VENTAS from STA11 where COD_ARTICU = '" & Me.DataGridView2.Item(0, i).Value() & "')" & _
                            ", (SELECT ID_MEDIDA_STOCK from STA11 where COD_ARTICU = '" & Me.DataGridView2.Item(0, i).Value() & "')" & _
                            ",null" & _
                             ", 'P')"
                Cmd = New SqlClient.SqlCommand(sbQuery, cn)
                Cmd.ExecuteNonQuery()
            Next
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub
End Class